home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / tracer / tracer.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  10KB  |  691 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: tracer.c,v 4.5 1997/06/25 21:45:06 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         Tracer version 1.0:  A Trace File Generator for PVM
  7.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  8.  *           Authors:  James Arthur Kohl and G. A. Geist
  9.  *                   (C) 1994 All Rights Reserved
  10.  *
  11.  *                              NOTICE
  12.  *
  13.  * Permission to use, copy, modify, and distribute this software and
  14.  * its documentation for any purpose and without fee is hereby granted
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both the copyright notice and this permission notice appear
  17.  * in supporting documentation.
  18.  *
  19.  * Neither the Institution, Oak Ridge National Laboratory, nor the
  20.  * Authors make any representations about the suitability of this
  21.  * software for any purpose.  This software is provided ``as is''
  22.  * without express or implied warranty.
  23.  *
  24.  * Tracer was funded by the U.S. Department of Energy.
  25.  */
  26.  
  27.  
  28. /* Tracer Headers */
  29.  
  30. #include "tracer.h"
  31.  
  32. #include "trcglob.h"
  33.  
  34. #ifdef NEEDSSELECTH 
  35. #include <sys/select.h>
  36. #endif
  37.  
  38.  
  39. /* Event Receiving Loop Limit */
  40.  
  41. #define EVENT_COUNT    20
  42.  
  43.  
  44. /* Command Routines */
  45.  
  46. void quit_proc();
  47.  
  48. int mask_proc();
  49.  
  50.  
  51. /* Handler Routines */
  52.  
  53. void my_status_msg();
  54. void program_init();
  55. void read_args();
  56. void pvm_init();
  57. void trc_init();
  58. void usage();
  59.  
  60. char *getenv();
  61.  
  62.  
  63. /* MAIN */
  64.  
  65. int
  66. main( argc, argv )
  67. int argc;
  68. char **argv;
  69. {
  70.     /* Read Command Line Args */
  71.  
  72.     read_args( argc, argv );
  73.  
  74.     /* Initialize Program Constants & Structs */
  75.  
  76.     program_init();
  77.  
  78.     /* Initialize PVM */
  79.  
  80.     pvm_init();
  81.  
  82.     /* Initialize Tracer Globals */
  83.  
  84.     trc_init();
  85.  
  86.     /* Do Tracer-like Stuff */
  87.  
  88.     work();
  89. }
  90.  
  91.  
  92. int
  93. work()
  94. {
  95.     struct timeval more_events;
  96.  
  97.     struct timeval *timeout;
  98.  
  99.     char cmd[1024];
  100.  
  101.     char *prompt;
  102.  
  103.     fd_set rfds;
  104.     fd_set fds;
  105.  
  106.     int *np;
  107.  
  108.     int nfds;
  109.     int more;
  110.     int max;
  111.     int in;
  112.     int n;
  113.     int i;
  114.  
  115.     /* Initialize File Descriptors */
  116.  
  117.     FD_ZERO( &rfds );
  118.  
  119.     /* Get stdin input */
  120.  
  121.     in = fileno( stdin );
  122.  
  123.     FD_SET( in, &rfds );
  124.  
  125.     nfds = 1;
  126.  
  127.     /* Get PVM Sockets */
  128.  
  129.     if ( pvm_getfds( &np ) > 0 )
  130.     {
  131.         FD_SET( np[0], &rfds );
  132.  
  133.         nfds = np[0] + 1;
  134.     }
  135.  
  136.     /* Initialize Select Timeout - For Events Still Pending */
  137.  
  138.     more_events.tv_sec = 0;
  139.     more_events.tv_usec = 0;
  140.  
  141.     /* Print Initial Prompt */
  142.  
  143.     prompt = "tracer> ";
  144.  
  145.     printf( prompt );
  146.     fflush( stdout );
  147.  
  148.     /* Process Events Messages and User Input */
  149.  
  150.     while ( 1 )
  151.     {
  152.         /* Check for Event Messages */
  153.  
  154.         more = recv_events();
  155.  
  156.         if ( more )
  157.             timeout = &more_events;
  158.         
  159.         else
  160.             timeout = (struct timeval *) NULL;
  161.  
  162.         /* Wait for Input */
  163.  
  164.         fds = rfds;
  165.  
  166.         if ( (n = SELECT( nfds, &fds, NULL, NULL, timeout )) == -1 )
  167.             perror( "select" );
  168.  
  169.         else if ( n > 0 && FD_ISSET( in, &fds ) )
  170.         {
  171.             if ( (n = read( in, cmd, sizeof( cmd ) - 1 )) < 1 )
  172.             {
  173.                 printf( "Quit\n" );
  174.  
  175.                 quit_proc();
  176.             }
  177.  
  178.             else
  179.             {
  180.                 cmd[ n ] = '\0';
  181.  
  182.                 handle_cmd( cmd );
  183.  
  184.                 printf( prompt );
  185.                 fflush( stdout );
  186.             }
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192. int
  193. handle_cmd( cmd )
  194. char *cmd;
  195. {
  196.     char *av[128];
  197.  
  198.     int ac;
  199.  
  200.     ac = ( sizeof( av ) / sizeof( av[0] ) ) - 1;
  201.  
  202.     if ( acav( cmd, &ac, av ) )
  203.     {
  204.         printf( "\nError:  Command Too Long...\n\n" );
  205.  
  206.         return( TRC_FALSE );
  207.     }
  208.  
  209.     /* "Ugly Parse" Command Ops */
  210.  
  211.     if ( ac >= 1 )
  212.     {
  213.         if ( !strcmp( av[0], "mask" ) )
  214.             mask_proc( ac, av );
  215.  
  216.         else if ( !strcmp( av[0], "buffer" )
  217.             || !strcmp( av[0], "buf" ) )
  218.         {
  219.             buffer_proc( ac, av );
  220.         }
  221.  
  222.         else if ( !strcmp( av[0], "options" )
  223.             || !strcmp( av[0], "opt" ) )
  224.         {
  225.             options_proc( ac, av );
  226.         }
  227.  
  228.         else if ( !strcmp( av[0], "help" ) || !strcmp( av[0], "h" )
  229.             || !strcmp( av[0], "?" ) )
  230.         {
  231.             help_proc( ac, av );
  232.         }
  233.  
  234.         else if ( !strcmp( av[0], "q" ) || !strcmp( av[0], "quit" )
  235.             || !strcmp( av[0], "x" ) || !strcmp( av[0], "exit" ) )
  236.         {
  237.             quit_proc( ac, av );
  238.         }
  239.  
  240.         else
  241.         {
  242.             printf( "Unknown Command \"%s\".\n", av[0] );
  243.  
  244.             return( TRC_FALSE );
  245.         }
  246.     }
  247.  
  248.     return( TRC_TRUE );
  249. }
  250.  
  251.  
  252. int
  253. recv_events()
  254. {
  255.     int status;
  256.     int ecnt;
  257.  
  258.     ecnt = trc_recv_messages( ID, EVENT_COUNT, &status );
  259.  
  260.     if ( ecnt < 0 )
  261.         exit( -1 );
  262.  
  263.     if ( ecnt )
  264.         fflush( ID->trace_out );
  265.  
  266.     return( status & TRC_MSG_STATUS_MORE );
  267. }
  268.  
  269.  
  270. void
  271. read_args( argc, argv )
  272. int argc;
  273. char **argv;
  274. {
  275.     char tmp[1024];
  276.  
  277.     int i, j, k;
  278.     int do_usage;
  279.     int len;
  280.  
  281.     OUTPUT_FILE = (char *) NULL;
  282.  
  283.     TRACE_FILE = (char *) NULL;
  284.  
  285.     TRACE_BUF = 0;
  286.  
  287.     TRACE_OPT = PvmTraceFull;
  288.  
  289.     vflag = 0;
  290.  
  291.     do_usage = 0;
  292.  
  293.     for ( i=1 ; i < argc ; i++ )
  294.     {
  295.         if ( argv[i][0] == '-' )
  296.         {
  297.             k = i + 1;
  298.  
  299.             len = strlen( argv[i] );
  300.  
  301.             for ( j=0 ; j < len ; j++ )
  302.             {
  303.                 switch ( argv[i][j] )
  304.                 {
  305.                     case 'H':
  306.                     case 'h':
  307.                         usage(); break;
  308.  
  309.                     case 'T':
  310.                     {
  311.                         if ( TRACE_FILE != NULL )
  312.                             free( TRACE_FILE );
  313.  
  314.                         TRACE_FILE = trc_copy_str( argv[k++] );
  315.  
  316.                         break;
  317.                     }
  318.  
  319.                     case 'O':
  320.                     {
  321.                         if ( OUTPUT_FILE != NULL )
  322.                             free( OUTPUT_FILE );
  323.  
  324.                         OUTPUT_FILE = trc_copy_str( argv[k++] );
  325.  
  326.                         break;
  327.                     }
  328.  
  329.                     case 'B':
  330.                     {
  331.                         TRACE_BUF = atoi( argv[k++] );
  332.  
  333.                         break;
  334.                     }
  335.  
  336.                     case 'v': vflag++; break;
  337.  
  338.                     case '-': break;
  339.  
  340.                     default:
  341.                     {
  342.                         printf( "Unknown Option -%c\n", argv[i][j] );
  343.  
  344.                         do_usage++;
  345.  
  346.                         break;
  347.                     }
  348.                 }
  349.             }
  350.  
  351.             i = k - 1;
  352.         }
  353.  
  354.         else
  355.         {
  356.             if ( !strcmp( argv[i], "full" ) )
  357.                 TRACE_OPT = PvmTraceFull;
  358.  
  359.             else if ( !strcmp( argv[i], "time" ) )
  360.                 TRACE_OPT = PvmTraceTime;
  361.  
  362.             else if ( !strcmp( argv[i], "count" ) )
  363.                 TRACE_OPT = PvmTraceCount;
  364.  
  365.             else
  366.             {
  367.                 printf( "Unknown Option %s\n", argv[i] );
  368.  
  369.                 do_usage++;
  370.             }
  371.         }
  372.     }
  373.  
  374.     if ( do_usage )
  375.         usage();
  376. }
  377.  
  378.  
  379. void
  380. usage()
  381. {
  382.     printf( "\nusage:  tracer [ -T tracefile ] [ -O outfile ] " );
  383.     printf( "[ full | time | count ] [ -Hhev ]\n\n" );
  384.  
  385.     printf( "where:\n" );
  386.     printf( "------\n" );
  387.  
  388.     printf( "-T file\t   = Use \"file\" as Trace File\n" );
  389.  
  390.     printf( "-O file\t   = Use \"file\" as Additional Output File\n" );
  391.  
  392.     printf( "-B nbytes\t   = Set Trace Buffer Size to nbytes\n" );
  393.  
  394.     printf( "full\t   = Set Tracing Option to Full\n" );
  395.     printf( "time\t   = Set Tracing Option to Timing\n" );
  396.     printf( "count\t   = Set Tracing Option to Count\n" );
  397.  
  398.     printf( "-H/h\t   = Print This Help Information\n" );
  399.  
  400.     printf( "-e\t   = Dump Raw PVM Event Text\n" );
  401.     printf( "-v\t   = Verbose Operation\n" );
  402.  
  403.     printf( "\n" );
  404.  
  405.     exit( 0 );
  406. }
  407.  
  408.  
  409. void
  410. program_init()
  411. {
  412.     struct passwd *pw;
  413.  
  414.     char hname[1024];
  415.     char pvstr[255];
  416.     char tmp[2048];
  417.  
  418. #ifdef WIN32
  419.     char *username = 0;
  420.     char *pstr;
  421. #endif
  422.  
  423.     char *stripped;
  424.     char *uppered;
  425.     char *home;
  426.  
  427.     int entry_exit;
  428.     int release;
  429.     int version;
  430.     int fmt;
  431.     int i;
  432.  
  433.     /* Get User Name for Trace File */
  434.  
  435.     if ( TRACE_FILE == NULL )
  436.     {
  437. #ifndef WIN32
  438.         if ( (pw = getpwuid( getuid() )) != NULL )
  439.             sprintf( tmp, "/tmp/tracefile.%s", pw->pw_name );
  440. #else
  441.         if ( !username )
  442.             username = (char *) MyGetUserName();
  443.  
  444.         if ( username )
  445.         {
  446.             if ( !(pstr = getenv( "PVM_TMP" )) )
  447.             {
  448.                 if ( !(pstr = getenv( "TEMP" )) )
  449.                     pstr = "c:\temp";
  450.             }
  451.             
  452.             sprintf( tmp, "%s.%s", pstr, username );
  453.         }
  454. #endif        
  455.         else
  456. #ifndef WIN32 
  457.             sprintf( tmp, "/tmp/tracefile.%d", getpid() );
  458. #else
  459.         {    
  460.             fprintf( stderr,
  461.                 "You are not logged onto the machine!  Exiting.\n");    
  462.             exit( -1 );
  463.         }
  464. #endif        
  465.         TRACE_FILE = trc_copy_str( tmp );
  466.     }
  467.  
  468.     /* Initialize Globals & Flags */
  469.  
  470.     TRACER_REGISTERED = TRC_FALSE;
  471. }
  472.  
  473.  
  474. void
  475. pvm_init()
  476. {
  477.     char *av[2];
  478.  
  479.     int inum;
  480.     int se;
  481.     int cc;
  482.     int i;
  483.  
  484.     /* Options */
  485.  
  486.     pvm_setopt( PvmResvTids, 1 );
  487.  
  488.     pvm_setopt( PvmRoute, PvmDontRoute );
  489.  
  490.     /* Get My TID */
  491.  
  492.     MYTID = pvm_mytid();
  493.  
  494.     if ( MYTID < 0 )
  495.     {
  496.         pvm_perror( "Error Joining PVM" );
  497.  
  498.         exit( -1 );
  499.     }
  500.  
  501.     else
  502.         printf( "\nTracer connected as tid=0x%x.\n", MYTID );
  503.  
  504.     /* Set Desired Signals */
  505.  
  506.     pvm_setopt( PvmNoReset, 1 );
  507.  
  508. #ifndef WIN32
  509.     signal( SIGALRM, SIG_IGN );
  510.     signal( SIGQUIT, (vfp) quit_proc );
  511. #endif
  512.  
  513.     signal( SIGINT, (vfp) quit_proc );
  514.     
  515.     /* Set Trace Masks */
  516.  
  517.     TEV_MASK_INIT( CLEAR_MASK );
  518.  
  519.     av[0] = "mask";
  520.     av[1] = "xpvm";
  521.  
  522.     mask_proc( 2, av );
  523.  
  524.     pvm_settmask( PvmTaskChild, TRACE_MASK );
  525.  
  526.     /* Set Trace Buffering */
  527.  
  528.     pvm_setopt( PvmTraceBuffer, TRACE_BUF );
  529.  
  530.     /* Set Tracing Options */
  531.  
  532.     pvm_setopt( PvmTraceOptions, TRACE_OPT );
  533. }
  534.  
  535.  
  536. void
  537. trc_init()
  538. {
  539.     /* Initialize Tracer */
  540.  
  541.     trc_tracer_init();
  542.  
  543.     /* Set Tracer Globals */
  544.  
  545.     TRC_HOST_ADD_NOTIFY_CODE = 99;
  546.     TRC_HOST_DEL_NOTIFY_CODE = 100;
  547.  
  548.     TRC_VERSION = MYVERSION;
  549.  
  550.     TRC_NAME = "Tracer";
  551.  
  552.     TRC_TID = MYTID;
  553.  
  554.     /* Get Tracer ID */
  555.  
  556.     ID = trc_get_tracer_id();
  557.  
  558.     /* Set Local Handler Routines */
  559.  
  560.     ID->status_msg = my_status_msg;
  561.  
  562.     /* Set Trace Event Message Codes */
  563.  
  564.     ID->event_ctx = pvm_getcontext();
  565.     ID->event_tag = 666;
  566.  
  567.     ID->output_ctx = pvm_getcontext();
  568.     ID->output_tag = 667;
  569.  
  570.     /* Set PVM Tracing Options */
  571.  
  572.     trc_set_tracing_codes( ID );
  573.  
  574.     /* Open Trace File */
  575.  
  576.     trc_set_trace_file( ID, TRACE_FILE );
  577.  
  578.     if ( !trc_reset_trace_file( ID ) )
  579.         exit( -1 );
  580.  
  581.     /* Check Hosts */
  582.  
  583.     trc_initialize_hosts( ID );
  584.  
  585.     /* Dump Host Status Events */
  586.  
  587.     trc_save_host_status_events( ID );
  588.  
  589.     /* Open Additional Output File */
  590.  
  591.     if ( OUTPUT_FILE != NULL )
  592.     {
  593.         trc_set_output_file( ID, OUTPUT_FILE );
  594.  
  595.         trc_open_output_file( ID );
  596.     }
  597. }
  598.  
  599.  
  600. /* Status Message Handler */
  601.  
  602. void my_status_msg( ID, msg )
  603. TRC_ID ID;
  604. char *msg;
  605. {
  606.     printf( "%s\n", msg );
  607. }
  608.  
  609.  
  610. /* Stolen from regular PVM console...  :-) */
  611.  
  612. /*    acav()
  613. *
  614. *    Parse a string into words separated by whitespace.
  615. *    Max number of words is original value of *acp.
  616. *
  617. *    Trashes out the original string.
  618. *    Returns 0 with av[0]..av[*acp - 1] pointing to the words.
  619. *    Returns 1 if too many words.
  620. *    Returns -1 if unbalanced quote.
  621. */
  622.  
  623. int
  624. acav(s, acp, av)
  625.     char *s;            /* the string to parse */
  626.     int *acp;            /* max num words in, num words found out */
  627.     char **av;            /* pointers to words */
  628. {
  629.     int ac = 0;            /* number of words found */
  630.     char *p = s;        /* input scanner */
  631.     char *q;            /* output */
  632.     int n = *acp;        /* max number of words allowed */
  633.     int mode = 0;        /* quote mode */
  634.  
  635.     while (*p) {
  636.         while (isspace(*p)) p++;
  637.         if (*p) {
  638.             if (*p == '#')
  639.                 break;
  640.             if (ac >= n) {
  641.                 *acp = ac;
  642.                 return 1;
  643.             }
  644.             q = p;
  645.             av[ac++] = p;
  646.             while (*p) {
  647.                 if (mode) {
  648.                     if (mode == '\\') {
  649.                         *q++ = *p;
  650.                         mode = 0;
  651.  
  652.                     } else if (mode == *p) {
  653.                         mode = 0;
  654.  
  655.                     } else
  656.                         *q++ = *p;
  657.  
  658.                 } else {
  659.                     if (isspace(*p))
  660.                         break;
  661.  
  662.                     switch (*p) {
  663.  
  664.                     case '"':
  665.                     case '\'':
  666.                     case '\\':
  667.                         mode = *p;
  668.                         break;
  669.  
  670.                     default:
  671.                         *q++ = *p;
  672.                         break;
  673.                     }
  674.                 }
  675.                 p++;
  676.             }
  677.             if (*p)
  678.                 p++;
  679.             if (*q)
  680.                 *q = 0;
  681.             if (mode) {
  682.                 printf("unmatched %c\n", (char)mode);
  683.                 return -1;
  684.             }
  685.         }
  686.     }
  687.     *acp = ac;
  688.     return 0;
  689. }
  690.  
  691.